package coreservlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * Servlet that gives browser-specific insult. Illustrates how to use the
 * User-Agent header to tell browsers apart.
 */
public class BrowserInsult extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String title, message;
		// Assume for simplicity that Netscape and IE are
		// the only two browsers.
		String userAgent = request.getHeader("User-Agent");
		if ((userAgent != null)) {
			if ((userAgent.indexOf("MSIE") != -1)) {
				title = "Microsoft Minion";
				message = "Welcome, O spineless slave to the "
						+ "mighty empire.";

			} else if ((userAgent.indexOf("Chrome") != -1)) {
				title = "Rangeela Chrome";
				message = "Wailt till it lasts. "
						+ "The world <I>will</I> produce a rival!";
			} else {
				title = "Hopeless Netscape Rebel";
				message = "Enjoy it while you can. "
						+ "You <I>will</I> be assimilated!";
			}
		} else {
			title = "NO name";
			message = "Why didn't you supply your name, you stupid agent!";
		}

		System.out.println(userAgent);
		String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
				+ "Transitional//EN\">\n";
		out.println(docType + "<HTML>\n" + "<HEAD><TITLE>" + title
				+ "</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n"
				+ "<H1 ALIGN=CENTER>" + title + "</H1>\n" + message + "\n"
				+ "</BODY></HTML>");
	}
}
